package com.itheima.blog.util;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class DateUtil {
    // 时间戳
    public static long getTimeStamp() {
        Date d = new Date();
        //getTime()得到的是微秒， 需要换算成秒
        long timeStamp = d.getTime() / 1000;
        return timeStamp;
    }


    /**
     * 获取当前时间前几分钟
     * @return
     */
    public static String getCurrentTime(int x){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -x);// x分钟之前的时间
        Date beforeD = beforeTime.getTime();
        String time = sdf.format(beforeD);
        return time;
    }


    /**
     * 按照format格式化日期
             * 按照format格式化日期
     * @param format
     * @return String
     * @author	HuMengmeng
     * @date 2020年1月14日下午4:34:13
     */
    public static String dateToString(String format){
    	Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
    public static String dateToString(){
    	Date date = new Date();
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	return sdf.format(date);
    }
    public static String dateToString(Date date){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	return sdf.format(date);
    }
    public static String dateToString(Date date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
    public static String getPreDateToString(int month){
    	Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH,-month);
		Date date = calendar.getTime();
		return DateUtil.dateToString(date);
    }
    
    /**
          * 获取昨天日期
	* @return String
	* @author	HuMengmeng
	* @date 2020年2月7日下午2:34:13
	*/
    public static String getYesterdayFormatDate(){
    	Calendar cal=Calendar.getInstance();
    	//获取昨天日期
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();
        SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd");
        return sp.format(d);
    }
    public static Date getYesterdayDate(){
    	Calendar cal=Calendar.getInstance();
    	//获取昨天日期
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();
        return d;
    }

    /**
     * 获取当前周的第一天(周一)
     * @Author 赵腾飞
     * @Date 2020/4/22 17:16
     **/
    public static String getFirstDayOfWeek(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) ;
        if (dayOfWeek == 1) {//说明是个周日
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) - 6);
        }else{
            cal.set(Calendar.DAY_OF_WEEK, 1);//查出本周的周日
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 1);
        }
        return format.format(cal.getTime());
    }

    /**
     * 获取当前周最后一天（周日）
     * @Author 赵腾飞
     * @Date 2020/4/22 17:16
     **/
    public static String getLastDayOfWeek(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) ;
        if (dayOfWeek == 1) {//说明是个周日
            cal.set(Calendar.DAY_OF_WEEK, 1);//查出本周的周日
            cal.set(Calendar.DATE, cal.get(Calendar.DATE));
        }else{
            cal.set(Calendar.DAY_OF_WEEK, 1);//查出本周的周日
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 7);
        }
        return format.format(cal.getTime());
    }

    /**
     * 获取当前月第一天
     * @Author 赵腾飞
     * @Date 2020/4/22 17:16
     **/
    public static String getCurrentMonthFirstDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(cale.getTime());
    }

    /**
     * 获取当前月最后一天
     * @Author 赵腾飞
     * @Date 2020/4/22 17:17
     **/
    public static String getCurrentMonthLastDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return format.format(cale.getTime());
    }

    /**
     * 获取当前年第一天
     * @Author 赵腾飞
     * @Date 2020/4/22 17:16
     **/
    public static String getCurrentYearFirstDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        int currentYear = cale.get(Calendar.YEAR);
        cale.clear();
        cale.set(Calendar.YEAR, currentYear);
        return format.format(cale.getTime());
    }

    /**
     * 获取当前年最后一天
     * @Author 赵腾飞
     * @Date 2020/4/22 17:17
     **/
    public static String getCurrentYearLastDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        int currentYear = cale.get(Calendar.YEAR);
        cale.clear();
        cale.set(Calendar.YEAR, currentYear);
        cale.roll(Calendar.DAY_OF_YEAR, -1);
        return format.format(cale.getTime());
    }

    public static List<String> getTodayHours() {
        Calendar cale = Calendar.getInstance();
        int currentHour = cale.get(Calendar.HOUR_OF_DAY);
        List<String> list = new ArrayList();
        if(currentHour > 10){
            for (int i = 10; i <currentHour ; i++) {
                list.add(String.valueOf(i));
            }
        }
        System.out.println(currentHour);
        return list;
    }

    /**
     * @Description 按照指定格式日期转字符串
     * @Author HuMengmeng
     * @Date  2020/7/23 0023 15:00
     **/
    public static String dateToString2(Date date,String format){

        SimpleDateFormat sdf=new SimpleDateFormat(format);

        String str=sdf.format(date);

        return str;
    }





}